package cn.iocoder.yudao.module.wine.service.commentreport;

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.wine.controller.app.commentreport.vo.AppCommentReportSaveReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.wine.controller.admin.commentreport.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.commentreport.CommentReportDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.wine.dal.mysql.commentreport.CommentReportMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wine.enums.ErrorCodeConstants.*;

/**
 * 评论举报记录 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class CommentReportServiceImpl implements CommentReportService {

    @Resource
    private CommentReportMapper commentReportMapper;

    @Override
    public Long createCommentReport(CommentReportSaveReqVO createReqVO) {
        // 插入
        CommentReportDO commentReport = BeanUtils.toBean(createReqVO, CommentReportDO.class);
        commentReportMapper.insert(commentReport);
        // 返回
        return commentReport.getId();
    }

    @Override
    public void updateCommentReport(CommentReportSaveReqVO updateReqVO) {
        // 校验存在
        validateCommentReportExists(updateReqVO.getId());
        // 更新
        CommentReportDO updateObj = BeanUtils.toBean(updateReqVO, CommentReportDO.class);
        commentReportMapper.updateById(updateObj);
    }

    @Override
    public void deleteCommentReport(Long id) {
        // 校验存在
        validateCommentReportExists(id);
        // 删除
        commentReportMapper.deleteById(id);
    }

    private void validateCommentReportExists(Long id) {
        if (commentReportMapper.selectById(id) == null) {
            throw exception(INFO_NOT_EXISTS);
        }
    }

    @Override
    public CommentReportDO getCommentReport(Long id) {
        return commentReportMapper.selectById(id);
    }

    @Override
    public PageResult<CommentReportRespVO> getCommentReportPage(CommentReportPageReqVO pageReqVO) {
        return commentReportMapper.selectPage(pageReqVO);
    }

    @Override
    public Long appCreateCommentReport(AppCommentReportSaveReqVO createReqVO) {
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        createReqVO.setUserId(loginUserId);
        CommentReportDO commentReport = BeanUtils.toBean(createReqVO, CommentReportDO.class);
        commentReportMapper.insert(commentReport);
        return commentReport.getId();
    }

}
